<?php

/**
 * This is the model class for table "site_administrator".
 *
 * The followings are the available columns in table 'site_administrator':
 * @property string $administrator_id
 * @property string $administrator_administrator_group_id
 * @property string $administrator_username
 * @property string $administrator_password
 * @property string $administrator_name
 * @property string $administrator_phone
 * @property string $administrator_image
 * @property string $administrator_last_login
 * @property string $administrator_is_active
 *
 * The followings are the available model relations:
 * @property SiteAdministratorGroup $administratorAdministratorGroup
 * @property SiteNews[] $siteNews
 */
class site_administrator extends CActiveRecord {

    public $administrator_new_password;
    public $administrator_confirm_password;

    /**
     * Returns the static model of the specified AR class.
     * @return site_administrator the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'site_administrator';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('administrator_administrator_group_id', 'length', 'max' => 10),
            array('administrator_username', 'length', 'max' => 50),
            array('administrator_username', 'filter', 'filter' => 'strtolower'), // convert username to lower case

            array('administrator_new_password, administrator_confirm_password', 'length', 'min' => 6, 'max' => 100, 'allowEmpty' => false, 'on' => 'insert'),
            array('administrator_new_password, administrator_confirm_password', 'length', 'min' => 6, 'max' => 100, 'allowEmpty' => true, 'on' => 'update'),
            array('administrator_new_password', 'compare', 'compareAttribute' => 'administrator_confirm_password', 'on' => 'insert'), // compare password to repeated password
            array('administrator_new_password', 'unsafe'),
            array('administrator_name, administrator_phone', 'length', 'max' => 255),
            array('administrator_image', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => true, 'maxSize' => 1024 * 1024 * 1, 'tooLarge' => 'Max filesize 1MB'),
            array('administrator_image', 'unsafe'),
            array('administrator_is_active', 'length', 'max' => 1),
            array('administrator_last_login', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('administrator_id, administrator_administrator_group_id, administrator_username, administrator_password, administrator_name, administrator_phone, administrator_image, administrator_last_login, administrator_is_active', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'rel_administrator_to_administrator_group' => array(self::BELONGS_TO, 'site_administrator_group', 'administrator_administrator_group_id'),
            'rel_administrator_to_news' => array(self::HAS_MANY, 'site_news', 'news_admin_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'administrator_id' => 'Administrator',
            'administrator_administrator_group_id' => 'Group',
            'administrator_username' => 'Username',
            'administrator_password' => 'Password',
            'administrator_confirm_password' => 'Confirm Password',
            'administrator_name' => 'Name',
            'administrator_phone' => 'Phone',
            'administrator_image' => 'Image',
            'administrator_last_login' => 'Last Login',
            'administrator_is_active' => 'Is Active',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        if (Yii::app()->user->administrator_group_type != 'superuser') {
            $criteria->with = array('rel_administrator_to_administrator_group');
            $criteria->condition = 'rel_administrator_to_administrator_group.administrator_group_type = :type';
            $criteria->params = array(':type' => 'administrator');
        }
        $criteria->compare('administrator_id', $this->administrator_id, true);
        $criteria->compare('administrator_administrator_group_id', $this->administrator_administrator_group_id, true);
        $criteria->compare('administrator_username', $this->administrator_username, true);
        $criteria->compare('administrator_password', $this->administrator_password, true);
        $criteria->compare('administrator_name', $this->administrator_name, true);
        $criteria->compare('administrator_phone', $this->administrator_phone, true);
        $criteria->compare('administrator_image', $this->administrator_image, true);
        $criteria->compare('administrator_last_login', $this->administrator_last_login, true);
        $criteria->compare('administrator_is_active', $this->administrator_is_active, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * @return actions to perform before saving ie: hash password
     */
    public function beforeSave() {
        if (!empty($this->administrator_new_password)) {
            $password_decrypt = md5($this->administrator_new_password);
            $this->administrator_password = $password_decrypt;
        }
        return true;
    }

}